

/*------------------------------------------------------------------------------
	1. Housekeeping
------------------------------------------------------------------------------*/

	*Run housekeeping code
		qui do ".../replication_package/housekeeping.do"

	*Write log
		log using "$log_loc/figure_a5.log", text replace

/*------------------------------------------------------------------------------
	2. Clean Main Datasets
------------------------------------------------------------------------------*/

		*Import self claim data
			use "$data/self_claim_2018.dta", clear
				destring id, replace
			tempfile self_claim
			save `self_claim', replace

		*Import Data
		use "$data/childrens2018.dta", clear

			/*Merge self-claim dummy*/
				fmerge 1:1 id using `self_claim'
					assert _merge != 2
					gen flag_self_claim = _merge == 3
					drop _merge 


			/*Restrict Sample*/
				*Keep 1095 filings
					rename state_insurance state
					fmerge m:1 state using "$data/statelist.dta"
						assert _merge != 2 //Confirm that all 51 states are in dataset
						drop if _merge != 3
						drop _merge

				*Keep if age_num is within the range
					keep if age_num >= 0 & age_num <= 16

				*Keep if kid died after end of year or is currently alive
					keep if 2018*10000+1231<death_date|death_date==0

				*Keep if months covered is possible
					keep if num_cov_ins > 0 & num_cov_ins <= 12

			/*Generate variables for figures*/	
				*Generate outcame variable on claiming
					gen onreturn = file_inc != .

				*Generate a count variable
					gen ct = 1

				*Collapse to the by-age_num level
					collapse (sum) ct onreturn flag_self_claim, by(age_num)

				*Adjust for claiming limits
					replace onreturn = onreturn * 1.0017029

				*Generate outcome (i.e., percent of children claimed)
					gen perc_return_self_clm = flag_self_claim/ct
					gen perc_return_all = (onreturn + flag_self_claim)/ct
			
				*Adjust outcome to be out of 100
					replace perc_return_self_clm  = perc_return_self_clm * 100
					replace perc_return_all = perc_return_all * 100

/*------------------------------------------------------------------------------
	3. Create Figure
------------------------------------------------------------------------------*/

			/*Create Figures*/	
				*Create Graph
				twoway 	(connected perc_return_self_clm age_num, msymbol(o) mlcolor(gs1) mfcolor(white) clcolor(gs1) lpattern(dash)) , ///
					ylabel(, format(%5.6f) labsize(small)) ///
					xtitle("Age", height(4)) ///
					xlabel(0(1)16, labsize(small)) ///	
					ytitle("Share That Claimed Themselves (%)", size(medsmall)) ///
					legend(off) ///
					graphregion(fcolor(white) color(white) lcolor(white))

				*Export Graph
					graph export "${output}/2018 self claim.png", replace as(png)




				*Create Graph
				twoway 	(connected perc_return_all age_num, msymbol(o) mlcolor(gs1) mfcolor(white) clcolor(gs1) lpattern(dash)) , ///
					ylabel(, format(%5.1f) labsize(small)) ///
					xtitle("Age", height(4)) ///
					xlabel(0(1)16, labsize(small)) ///	
					ytitle("Share On Any Return (%)", size(medsmall)) ///
					legend(off) ///
					graphregion(fcolor(white) color(white) lcolor(white))

				*Export Graph
					graph export "${output}/2018 self claim or claimed.png", replace as(png)

